package com.hsxc.cdz.monitor.bean.extend.pile.message;

import org.jdom2.IllegalDataException;

import com.hsxc.cdz.base.util.BytesUtil;
import com.hsxc.cdz.monitor.bean.extend.Msg;
import com.hsxc.cdz.monitor.bean.extend.pile.content.PileContent;

public abstract class PileMessage<T extends PileContent> extends Msg<T> {

	/***************************************************************/
	//帧代码常量
	// 桩上传心跳
	public static final String SEND_HEART = "51";
	// 桩上传遥变
    public static final String CLIENT_SEND_CHANGE = "53";
    // 桩上传账单
 	public static final String CLIENT_SEND_BILL = "54";
    // 桩主动上传开始充电
 	public static final String CLIENT_SEND_START = "11";
    // 桩主动上传停止充电请求
 	public static final String CLIENT_SEND_STOP = "13";
    // 桩 主动上传停止预约请求
 	public static final String CLIENT_SEND_STOP_ORDER = "17";
	/***************************************************************/
 	/**
	 * 从byte[]中获取报文并创建报文对象内容
	 * @param result1
	 * @throws Exception
	 */
	public void createMessage(byte[] result1) throws Exception{
		if(result1==null||result1.length!=headLength){
			throw new IllegalDataException("byte[]数据有误");
		}
		byte[] temp;
		int templength =0;	
		temp = BytesUtil.getByte(templength,ISOPEN_LENGTH,result1);
        this.setIsOpen(BytesUtil.binBytesToStr(temp));
        templength += ISOPEN_LENGTH;
        
        temp = BytesUtil.getByte(templength,CLENGTH_LENGTH, result1);
        this.setContentLength(BytesUtil.bytesToIntDesc(temp));
        templength += CLENGTH_LENGTH;
        
        temp = BytesUtil.getByte(templength,ISREGISTER_LENGTH,result1);
        this.setIsRegister(BytesUtil.binBytesToStr(temp));
        templength += ISREGISTER_LENGTH;
        
        temp = BytesUtil.getByte(templength,RESPONSE_LENGTH,result1);
        this.setResponse(BytesUtil.binBytesToStr(temp));
        templength += RESPONSE_LENGTH;
        
        temp = BytesUtil.getByte(templength, OPERATOR_LENGTH, result1);
        this.setOperator(BytesUtil.bcdBytesToStr(temp));
        templength += OPERATOR_LENGTH;
        
        temp = BytesUtil.getByte(templength, ADDRESS_LENGTH, result1);
        this.setAddress(BytesUtil.bcdBytesToStr(temp));
        templength += ADDRESS_LENGTH;
        
        temp = BytesUtil.getByte(templength, FRAME_LENGTH, result1);
        this.setFrame(BytesUtil.binBytesToStr(temp));
        templength += FRAME_LENGTH;
        
        temp = BytesUtil.getByte(templength,LINE_LENGTH, result1);
        this.setLineNum(BytesUtil.bcdBytesToStr(temp));
	}

	/**
	 *从byte[] 中获取并创建报文体对象
	 * @param data
	 * @throws Exception 
	 */
	public abstract void setContent(byte[] data) throws Exception;
}